package com.wm.controller;

import com.wm.entity.ResponseResult;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class WMException{
    @ExceptionHandler(DataAccessException.class)
    public ResponseResult<Void> handleException(){
        return ResponseResult.ERROR;
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<Void> handler(MethodArgumentNotValidException exception) {
        Map<String, String> msg = new HashMap<>();
        exception.getFieldErrors().forEach(e ->{
            System.out.println(e.getField());
            System.out.println(e.getDefaultMessage());
            msg.put(e.getField(),e.getDefaultMessage());
        });

        return new ResponseResult(888,"参数无效",msg);

    }

}
